package _0util;

import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.Query;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Set;

/**
 * @ClassName SystemUtil
 * @Description: 获取操作系统和应用信息的工具类
 * @Author wangzhen
 * @Date 2019/12/27
 **/
public class SystemUtil {
    static InetAddress inetAddress=null;


    static{
        try {
            inetAddress=InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            System.out.println("\033[31;4m" +e.getMessage()+ "\033[0m");
        }
    }

    /**
     * 返回当前主机IP
     * @return
     */
    public static String getHttpIP(){

        return inetAddress.getHostAddress();
    }


    public static String getLocalPort(){
        MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objectNames = null;
        try {
            objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),
                    Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }
        String port = objectNames.iterator().next().getKeyProperty("port");
        return port;
    }


    /**
     * 打印红色字体
     */
    public static void ConsoleRedFont(){
        System.out.println();
    }


    /**
     * 打印绿色字体
     * @param message
     */
    public static void ConsoleGreenFont(String message){
        if(message==null){
            message="";
        }
        System.out.println("\033[32;4m" + message + "\033[0m");
    }



    /**
     * 打印蓝色字体
     * @param message
     */
    public static void ConsoleBlueFont(String message){
        if(message==null){
            message="";
        }
        System.out.println("\033[34;4m" + message + "\033[0m");
    }




    /**
     * 打印紫色字体
     * @param message
     */
    public static void ConsoleVioletFont(String message){
        if(message==null){
            message="";
        }
        System.out.println("\033[35;4m" + message + "\033[0m");
    }




}
